package blog.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.entity.Article;
import blog.entity.ArticleCategoryRef;
import blog.entity.ArticleTagRef;
import blog.entity.Category;
import blog.entity.Tag;
import blog.enums.ArticleCommentStatus;
import blog.mapper.ArticleCategoryRefMapper;
import blog.mapper.ArticleMapper;
import blog.mapper.ArticleTagRefMapper;
import blog.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {
	@Resource
	private ArticleMapper articleMapper;
	
	@Resource
	private ArticleCategoryRefMapper articleCategoryRefMapper;
	
	@Resource
	private ArticleTagRefMapper articleTagRefMapper;
	/**
	 * 查询最近n条文章列表
	 */
	public List<Article> listRecentArticle(Integer n) {
		return articleMapper.listRecentArticle(n);
	}

	public PageInfo<Article> getPageArticleList(Integer pageIndex, Integer pageSize) {
		// 进行分页
		PageHelper.startPage(pageIndex, pageSize);

		List<Article> articleList = articleMapper.findAll();

		for (int i = 0; i < articleList.size(); i++) {
			// 封装CategoryList
			List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
			if (categoryList == null || categoryList.size() == 0) {
				categoryList = new ArrayList<>();
				categoryList.add(Category.getDefault());
			}
			articleList.get(i).setCategoryList(categoryList);
		}

		// 包装查出来的列表
		return new PageInfo<Article>(articleList);
	}
	
	public void addArticle(Article article) {
		// 添加文章
				article.setArticleCreateTime(LocalDateTime.now());
				article.setArticleUpdateTime(LocalDateTime.now());
				article.setArticleIsComment(ArticleCommentStatus.ALLOW.getValue());
				article.setArticleViewCount(0);
				article.setArticleLikeCount(0);
				article.setArticleCommentCount(0);
				article.setArticleOrder(1);
				articleMapper.addArticle(article);
				
				// 添加分类和文章关联
				for (int i = 0; i < article.getCategoryList().size(); i++) {
					ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
					articleCategoryRefMapper.addArticleCategory(articleCategoryRef);
				}
				// 添加标签和文章关联
				for (int i = 0; i < article.getTagList().size(); i++) {
					ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
					articleTagRefMapper.addArticleTag(articleTagRef);
				}
	}
	/**
	 * 根据ID查询文章信息
	 */
	@Override
	public Article getArticleById(Integer id) {
	   Article article = articleMapper.getArticleById(id);
        if (article != null) {
            List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(article.getArticleId());
            List<Tag> tagList = articleTagRefMapper.listTagByArticleId(article.getArticleId());
            article.setCategoryList(categoryList);
            article.setTagList(tagList);
        }
        return article;
	}
	@Override
	public void deleteArticle(Integer id) {
		Article article=getArticleById(id);
		articleTagRefMapper.deleteByArticleId(article.getArticleId());
		articleCategoryRefMapper.deleteByArticleId(article.getArticleId());
		articleMapper.deleteArticle(id);
	}
	
	

}
